/**
** range 插件
**author: ericever
**create time: 2015-7-24
**/
;(function($, window, document, undefined){
    'use strict';
    var old = $.fn.Anchor;
    var defaults = {
        min:13,
        max:65,
        lefVal:13,
        rigVal:20,
        init:null
    };
    var Anchor = function (element, options) {
        var self = this;
        this.$element = $(element);
        this.config = $.extend(true,{}, defaults, options); 
        var list = this.config.list;
        var min = this.config.min;
        var max = this.config.max;
        var lf = this.config.lefVal;
        var ri = this.config.rigVal;
        var init = this.config.init;
        function build(){
            var len = $(element).length;
            for(var i=0;i<len;i++){
                var the = $($(element)[i]);
                if(the.children().length>0){
                    the.find("input:first").val(lf);
                    the.find("input:last").val(ri);
                    return false;    
                };
                var box = $('<div class="dropdown">');
                box.append('<input class="form-control" data-toggle="dropdown" readOnly="true" aria-expanded="true">');
                box.append('<span class="caret" data-toggle="dropdown"></span>');
                box.append('<ul class="dropdown-menu">');
                if(lf) box.find("input").val(lf);
                for(var i=min;i<max+1;i++){
                   box.find("ul").append('<li><a>'+i+'</a></li>'); 
                };
                if(init && typeof(init) == "function") init(min,max);
                (function(the,box){
                    var cla = the.hasClass("year-range");
                    box.find("ul>li").unbind("click").click(function(){
                        var txt = $(this).text();
                        var inp = $(this).parent().siblings("input");
                        if(txt!=inp.val()){
                            inp.val(txt);
                            var val1 = the.find("input:first").val();
                            var val2 = the.find("input:last").val();
                            if(!val2) val2 = max;
                            if(val1&&val2&&(val2<val1)){
                                the.find("input:first").val(val2);
                                the.find("input:last").val(val1);
                            }
                            if(cla && val1 && !val2){
                                the.find("input:last").val(max);
                            }
                            if(!val2) {
                                val2 = max;
                            }
//                            var arr = [val1,val2]; 
                            the.trigger("change",{min:Math.min(val1,val2),max:Math.max(val1,val2)});
                        }
                    });                
                }(the,box));
                var box2 = box.clone(true);
                if(ri) box2.find("input").val(ri);
                the.append(box).append('&nbsp;<span>￣</span>&nbsp;').append(box2);
            }

        };
        
        function destory(){
            $(element).empty();
        };
        
        this.startup = function(){
            build();
        }
    };
    
    /***
    ** 入口
    **/
    $.fn.ranger = function(options){
        var bd = new Anchor(this,options);
        bd.startup();
        return this;
    }
}(window.j$ || jQuery, window, document));